#include "bsp_encode.h"
#include "tim.h"

Encoder Motor_1;

void Encoder_Init (void){
    HAL_TIM_Base_Start_IT(&htim7);                     //定时器中断开启

    HAL_TIM_Base_Start(&htim1);
    HAL_TIM_Encoder_Start(&htim1,TIM_CHANNEL_1);//定时器编码器模式开启
    HAL_TIM_Encoder_Start(&htim1,TIM_CHANNEL_2);
}

int16_t Encoder_Get(void){
    static int16_t Encoder_Cnt = 0;
    static int16_t Last_Encoder_Cnt = 0;
    int16_t Encoder_max = 65535;
    Last_Encoder_Cnt = Encoder_Cnt;
    Encoder_Cnt = __HAL_TIM_GET_COUNTER(&htim1);
    if ((float)abs(Encoder_Cnt - Last_Encoder_Cnt) > (float)Encoder_max*0.5f){
        if (Encoder_Cnt > Last_Encoder_Cnt){
            return -(Last_Encoder_Cnt+Encoder_max-Encoder_Cnt);
        }else{
            return Encoder_Cnt+Encoder_max-Last_Encoder_Cnt;
        }
    }
    return Encoder_Cnt-Last_Encoder_Cnt;
}







